// pages/order/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders:[],
    tabs: [{
      id: 0,
      value: "综合",
      isActive: true
    }, 
    {
      id: 1,
      value: "待付款",
      isActive: false
    }, 
    {
      id: 2,
      value: "待发货",
      isActive: false
    },
    {
      id: 3,
      value: "退款/退货",
      isActive: false
    }],
    listOrders:{
      "count": 11,
      "orders": [
        {
          "order_id": 428,
          "user_id": 23,
          "order_number": "HMDD20190802000000000428",
          "order_price": 13999,
          "order_pay": "0",
          "is_send": "否",
          "trade_no": "",
          "order_fapiao_title": "个人",
          "order_fapiao_company": "",
          "order_fapiao_content": "",
          "consignee_addr": "广东省广州市海珠区新港中路397号",
          "pay_status": "1",
          "create_time": 1564731518,
          "update_time": 1564731518,
          "order_detail": null,
          "goods": [
            {
              "id": 717,
              "order_id": 428,
              "goods_id": 43986,
              "goods_price": 13999,
              "goods_number": 1,
              "goods_total_price": 13999,
              "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
              "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
            }
          ],
          "total_count": 1,
          "total_price": 13999
        },
        {
          "order_id": 429,
          "user_id": 23,
          "order_number": "HMDD20190802000000000429",
          "order_price": 13999,
          "order_pay": "0",
          "is_send": "否",
          "trade_no": "",
          "order_fapiao_title": "个人",
          "order_fapiao_company": "",
          "order_fapiao_content": "",
          "consignee_addr": "广东省广州市海珠区新港中路397号",
          "pay_status": "1",
          "create_time": 1564731518,
          "update_time": 1564731518,
          "order_detail": null,
          "goods": [
            {
              "id": 717,
              "order_id": 428,
              "goods_id": 43986,
              "goods_price": 13999,
              "goods_number": 1,
              "goods_total_price": 13999,
              "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
              "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
            }
          ],
          "total_count": 1,
          "total_price": 13999
        },
        {
          "order_id": 430,
          "user_id": 23,
          "order_number": "HMDD20190802000000000430",
          "order_price": 13999,
          "order_pay": "0",
          "is_send": "否",
          "trade_no": "",
          "order_fapiao_title": "个人",
          "order_fapiao_company": "",
          "order_fapiao_content": "",
          "consignee_addr": "广东省广州市海珠区新港中路397号",
          "pay_status": "1",
          "create_time": 1564731518,
          "update_time": 1564731518,
          "order_detail": null,
          "goods": [
            {
              "id": 717,
              "order_id": 428,
              "goods_id": 43986,
              "goods_price": 13999,
              "goods_number": 1,
              "goods_total_price": 13999,
              "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
              "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
            }
          ],
          "total_count": 1,
          "total_price": 13999
        },{
          "order_id": 431,
          "user_id": 23,
          "order_number": "HMDD20190802000000000431",
          "order_price": 13999,
          "order_pay": "0",
          "is_send": "否",
          "trade_no": "",
          "order_fapiao_title": "个人",
          "order_fapiao_company": "",
          "order_fapiao_content": "",
          "consignee_addr": "广东省广州市海珠区新港中路397号",
          "pay_status": "1",
          "create_time": 1564731518,
          "update_time": 1564731518,
          "order_detail": null,
          "goods": [
            {
              "id": 717,
              "order_id": 428,
              "goods_id": 43986,
              "goods_price": 13999,
              "goods_number": 1,
              "goods_total_price": 13999,
              "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
              "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
            }
          ],
          "total_count": 1,
          "total_price": 13999
        },
        {
          "order_id": 432,
          "user_id": 23,
          "order_number": "HMDD20190802000000000432",
          "order_price": 13999,
          "order_pay": "0",
          "is_send": "否",
          "trade_no": "",
          "order_fapiao_title": "个人",
          "order_fapiao_company": "",
          "order_fapiao_content": "",
          "consignee_addr": "广东省广州市海珠区新港中路397号",
          "pay_status": "1",
          "create_time": 1564731518,
          "update_time": 1564731518,
          "order_detail": null,
          "goods": [
            {
              "id": 717,
              "order_id": 428,
              "goods_id": 43986,
              "goods_price": 13999,
              "goods_number": 1,
              "goods_total_price": 13999,
              "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
              "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
            }
          ],
          "total_count": 1,
          "total_price": 13999
        },
        {
          "order_id": 433,
          "user_id": 23,
          "order_number": "HMDD20190802000000000433",
          "order_price": 13999,
          "order_pay": "0",
          "is_send": "否",
          "trade_no": "",
          "order_fapiao_title": "个人",
          "order_fapiao_company": "",
          "order_fapiao_content": "",
          "consignee_addr": "广东省广州市海珠区新港中路397号",
          "pay_status": "1",
          "create_time": 1564731518,
          "update_time": 1564731518,
          "order_detail": null,
          "goods": [
            {
              "id": 717,
              "order_id": 428,
              "goods_id": 43986,
              "goods_price": 13999,
              "goods_number": 1,
              "goods_total_price": 13999,
              "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
              "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
            }
          ],
          "total_count": 1,
          "total_price": 13999
        },
        {
          "order_id": 434,
          "user_id": 23,
          "order_number": "HMDD20190802000000000434",
          "order_price": 13999,
          "order_pay": "0",
          "is_send": "否",
          "trade_no": "",
          "order_fapiao_title": "个人",
          "order_fapiao_company": "",
          "order_fapiao_content": "",
          "consignee_addr": "广东省广州市海珠区新港中路397号",
          "pay_status": "1",
          "create_time": 1564731518,
          "update_time": 1564731518,
          "order_detail": null,
          "goods": [
            {
              "id": 717,
              "order_id": 428,
              "goods_id": 43986,
              "goods_price": 13999,
              "goods_number": 1,
              "goods_total_price": 13999,
              "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
              "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
            }
          ],
          "total_count": 1,
          "total_price": 13999
        },
        {
          "order_id": 435,
          "user_id": 23,
          "order_number": "HMDD20190802000000000435",
          "order_price": 13999,
          "order_pay": "0",
          "is_send": "否",
          "trade_no": "",
          "order_fapiao_title": "个人",
          "order_fapiao_company": "",
          "order_fapiao_content": "",
          "consignee_addr": "广东省广州市海珠区新港中路397号",
          "pay_status": "1",
          "create_time": 1564731518,
          "update_time": 1564731518,
          "order_detail": null,
          "goods": [
            {
              "id": 717,
              "order_id": 428,
              "goods_id": 43986,
              "goods_price": 13999,
              "goods_number": 1,
              "goods_total_price": 13999,
              "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
              "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
            }
          ],
          "total_count": 1,
          "total_price": 13999
        },{
          "order_id": 436,
          "user_id": 23,
          "order_number": "HMDD20190802000000000436",
          "order_price": 13999,
          "order_pay": "0",
          "is_send": "否",
          "trade_no": "",
          "order_fapiao_title": "个人",
          "order_fapiao_company": "",
          "order_fapiao_content": "",
          "consignee_addr": "广东省广州市海珠区新港中路397号",
          "pay_status": "1",
          "create_time": 1564731518,
          "update_time": 1564731518,
          "order_detail": null,
          "goods": [
            {
              "id": 717,
              "order_id": 428,
              "goods_id": 43986,
              "goods_price": 13999,
              "goods_number": 1,
              "goods_total_price": 13999,
              "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
              "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
            }
          ],
          "total_count": 1,
          "total_price": 13999
        },
        {
          "order_id": 437,
          "user_id": 23,
          "order_number": "HMDD20190802000000000437",
          "order_price": 13999,
          "order_pay": "0",
          "is_send": "否",
          "trade_no": "",
          "order_fapiao_title": "个人",
          "order_fapiao_company": "",
          "order_fapiao_content": "",
          "consignee_addr": "广东省广州市海珠区新港中路397号",
          "pay_status": "1",
          "create_time": 1564731518,
          "update_time": 1564731518,
          "order_detail": null,
          "goods": [
            {
              "id": 717,
              "order_id": 428,
              "goods_id": 43986,
              "goods_price": 13999,
              "goods_number": 1,
              "goods_total_price": 13999,
              "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
              "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
            }
          ],
          "total_count": 1,
          "total_price": 13999
        },
        {
          "order_id": 438,
          "user_id": 23,
          "order_number": "HMDD20190802000000000438",
          "order_price": 13999,
          "order_pay": "0",
          "is_send": "否",
          "trade_no": "",
          "order_fapiao_title": "个人",
          "order_fapiao_company": "",
          "order_fapiao_content": "",
          "consignee_addr": "广东省广州市海珠区新港中路397号",
          "pay_status": "1",
          "create_time": 1564731518,
          "update_time": 1564731518,
          "order_detail": null,
          "goods": [
            {
              "id": 717,
              "order_id": 428,
              "goods_id": 43986,
              "goods_price": 13999,
              "goods_number": 1,
              "goods_total_price": 13999,
              "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
              "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
            }
          ],
          "total_count": 1,
          "total_price": 13999
        }
      ]
    },

  },
  handleTabsItemChange(e) {
    //获取被点击的标题
    const {
      index
    } = e.detail;
    //修改原数组
   this.changeTitleByIndex(index);

   //重新发送请求
   this.getOrders(index+1);
  },

  changeTitleByIndex(index){
    let {
      tabs
    } = this.data;
    tabs.forEach((v, i) => i === index ? v.isActive = 'true' : v.isActive = false);
    this.setData({
      tabs
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const token = wx.getStorageSync('token');

    //不含token则跳过授权获取

    // if(!token){
    //   wx.navigateTo({
    //     url: '/pages/auth/index',
    //   });
    //   return;
    // }
    //获取当前小程序的页面栈
    let pages = getCurrentPages();
    let currentPage = pages[pages.length-1];
    const {type} = currentPage.options;
    //激活选中
    this.changeTitleByIndex(type-1);
    this.getOrders(type);

  },

  async getOrders(type){
      // const res = await request({URL:"my/orders/all",data:{type}});
      const res = this.data.listOrders;
      this.setData({
        //遍历来见时间戳改为中国日期
       orders:res.orders.map(v=>({...v,create_time_cn:(new Date(v.create_time*1000).toLocaleString())}))
      })
  }
})